
from rest_framework import serializers

from questions.models import Questions, Answer, Lable
from users.models import User


class  QaSerializer(serializers.ModelSerializer):
    """ 返回问题表相关数据"""
<<<<<<< .merge_file_iieJAd
=======

>>>>>>> .merge_file_DcmWfd
    class  Meta:
        model =  Questions
        fields = '__all__'

<<<<<<< .merge_file_iieJAd
=======
    def validate(self, attrs):
        return attrs

    def create(self, validated_data):
        # 通过前端传的lable标签名,判断此标签是否存在,存在就取到id保存到question表,不存在,则创建该标签到标签表,并取得id
        for lable_name in  validated_data['lable']:
            li = [Lable.objects.filter(name=lable_name)]
            if lable_name in li:
                validated_data['lable'] = lable_name.id
            else:
                Lable.objects.create(lable_name)
                validated_data['lable'] = lable_name.id
         # 删除标签名
        del validated_data['lable']
        questions =  Questions.objects.create_questions(validated_data)

        return questions

>>>>>>> .merge_file_DcmWfd

class AnswerUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['username']

class AnswerSerializer(serializers.ModelSerializer):
    """ 回答 序列化器"""

    class Meta:
        model = Answer
        fields = ['answer_time','answer_user_id','answer_counts']



class LableSerializer(serializers.ModelSerializer):
    """ 回答 序列化器"""
    class Meta:
        model = Lable
        fields = ['name','id']

